#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;
using PLACEHR.Web.Categories.Management.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;

#endregion

namespace PLACEHR.Web.Categories.Management.Controls
{
    public partial class VacationBaseControl : UserControl, IHostedApplication
    {
        public VacationBaseControl()
        {
            InitializeComponent();
            InitialData();

            Retrieve();
        }

        public void InitializeApplication()
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(21), LanLayer.GetTrans(21), new IconResourceHandle("CRM.ico_16_5006.gif"), "OpenSelected"));
            objElements.Add(new HostedToolBarSeperator());

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            string strAction = (string)objButton.Tag;
            switch (strAction)
            {
                case "OpenSelected":
                    OpenDetailForm();
                    break;
            }
        }

        private void InitialData()
        {
            lblEmployeeName.Text = LanLayer.GetTrans(122) + ":";
            lblDepartment.Text = LanLayer.GetTrans(125) + ":";
            lblOffice.Text = LanLayer.GetTrans(124) + ":";
            lblEmployeeStatus.Text = LanLayer.GetTrans(192) + ":";

            colName.Text = LanLayer.GetTrans(122);
            colOffice.Text = LanLayer.GetTrans(124);
            colDepartment.Text = LanLayer.GetTrans(125);
            colANN.Text = LanLayer.GetTrans(194);
            colSKL.Text = LanLayer.GetTrans(207);
            colCSL.Text = LanLayer.GetTrans(208);
            colLSL.Text = LanLayer.GetTrans(209);

            cbbOffice.Items.Clear();
            cbbOffice.DataSource = Utility.GetOfficeList();
            cbbOffice.DisplayMember = "value";
            cbbOffice.ValueMember = "key";

            cbbDepartment.Items.Clear();
            cbbDepartment.DataSource = Utility.GetDeptList();
            cbbDepartment.DisplayMember = "value";
            cbbDepartment.ValueMember = "key";

            cbbEmployeeStatus.SelectedIndex = 0;


            if (Global.CurrentUser.RoleID == 2 || Global.CurrentUser.RoleID == 3)
            {
                var office = Global.CurrentUser.OfficeDepartment_lnk.FirstOrDefault().Office;
                cbbOffice.SelectedValue = office.OfficeCode;
                cbbOffice.Enabled = false;
            }
        }

        private void Retrieve()
        {
            try
            {
                var employeeList = Global.Context.Employees.Where(p => !p.IsDeleted && p.RoleID.HasValue && p.RoleID < 4).ToList();
                var vacationBaseList = Global.Context.VacationBases.Where(p => p.IsCurrent && p.IsDeleted == false).ToList();

                if (!string.IsNullOrWhiteSpace(cbbEmployeeStatus.Text))
                {
                    employeeList = employeeList.Where(p => p.StatusCode == cbbEmployeeStatus.Text).ToList();
                }

                if (!string.IsNullOrWhiteSpace(cbbOffice.Text))
                {
                    string officeCode = cbbOffice.SelectedValue.ToString();
                    employeeList = employeeList.Where(p => p.OfficeDepartment_lnk.Select(o => o.OfficeCode).FirstOrDefault() == officeCode).ToList();
                }

                if (!string.IsNullOrWhiteSpace(cbbDepartment.Text))
                {
                    employeeList = employeeList.Where(p => p.OfficeDepartment_lnk != null
                        && p.OfficeDepartment_lnk.Any(k => string.Equals(cbbDepartment.SelectedValue.ToString(), k.DepartmentCode, StringComparison.OrdinalIgnoreCase))).ToList();
                }

                if (!string.IsNullOrWhiteSpace(tbEmployeeName.Text))
                {
                    employeeList = employeeList.Where(p => p.EnglishName.ToLower().Contains(tbEmployeeName.Text.ToLower()) || p.ChineseName.ToLower().Contains(tbEmployeeName.Text.ToLower())).ToList();
                }

                lvwData.Items.Clear();
                lvwData.BeginUpdate();
                int No = 1;
                foreach (var employee in employeeList)
                {
                    ListViewItem lvwItem = new ListViewItem("");
                    lvwItem.SubItems.AddRange(new string[8]);

                    var officeDepartment_lnk = employee.OfficeDepartment_lnk.FirstOrDefault();
                    lvwItem.SubItems[0].Text = (No++).ToString();
                    lvwItem.SubItems[1].Text = employee.EnglishName + "(" + employee.ChineseName + ")";
                    lvwItem.SubItems[2].Text = officeDepartment_lnk == null ? "" : LanLayer.GetTrans(officeDepartment_lnk.Office.ContentId.Value);
                    lvwItem.SubItems[3].Text = officeDepartment_lnk == null ? "" : LanLayer.GetTrans(officeDepartment_lnk.Department.ContentId.Value);

                    var currVacationBaseList = vacationBaseList.Where(p => p.EmployeeID == employee.EmployeeID).ToList();
                    VacationBase ann = currVacationBaseList.FirstOrDefault(p => p.VacationTypeCode.Equals(Constants.VacationType_ANN, StringComparison.OrdinalIgnoreCase));
                    lvwItem.SubItems[4].Text = ann == null ? "0" : ann.FinalHours.ToString();

                    VacationBase skl = currVacationBaseList.FirstOrDefault(p => p.VacationTypeCode.Equals(Constants.VacationType_SKL, StringComparison.OrdinalIgnoreCase));
                    lvwItem.SubItems[5].Text = skl == null ? "0" : skl.FinalHours.ToString();

                    VacationBase csl = currVacationBaseList.FirstOrDefault(p => p.VacationTypeCode.Equals(Constants.VacationType_CSL, StringComparison.OrdinalIgnoreCase));
                    lvwItem.SubItems[6].Text = csl == null ? "0" : csl.FinalHours.ToString();

                    VacationBase lsl = currVacationBaseList.FirstOrDefault(p => p.VacationTypeCode.Equals(Constants.VacationType_LSL, StringComparison.OrdinalIgnoreCase));
                    lvwItem.SubItems[7].Text = lsl == null ? "0" : lsl.FinalHours.ToString();

                    lvwItem.Tag = employee;

                    lvwData.Items.Add(lvwItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OpenDetailForm()
        {
            if (lvwData.SelectedItem != null)
            {
                Employee item = lvwData.SelectedItem.Tag as Employee;
                VacationBaseForm form = new VacationBaseForm();
                form.currItem = item;
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
                form.ShowDialog();
            }
        }

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            OpenDetailForm();
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            VacationBaseForm form = sender as VacationBaseForm;
            if (form.DialogResult == DialogResult.OK)
            {
                Retrieve();
            }
        }

        private void queryControl1_FilterQueryClick(object sender, EventArgs e)
        {
            Retrieve();
        }
    }
}
